package com.ewzj.bigdata.mvp.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.ViewPager
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.di.component.DaggerNormalComponent
import com.ewzj.bigdata.di.module.NormalModule
import com.ewzj.bigdata.mvp.contract.NormalContract
import com.ewzj.bigdata.mvp.model.rspbean.DecPicBean
import com.ewzj.bigdata.mvp.presenter.NormalPresenter
import com.ewzj.bigdata.mvp.ui.holder.PhotoPreViewHolderView
import com.jakewharton.rxbinding2.view.RxView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import kotlinx.android.synthetic.main.activity_photopreview.*
import java.io.Serializable

class PhotoPreViewActivity : BaseActivity<NormalPresenter>(),NormalContract.View {
    lateinit var datas :List<DecPicBean.PicsEntity>
    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerNormalComponent.builder().appComponent(appComponent).normalModule(NormalModule(this))
                .build().inject(this)
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_photopreview
    }

    override fun initData(savedInstanceState: Bundle?) {
        datas=getIntent().getSerializableExtra(AppKey.EXTRA_DATA) as List<DecPicBean.PicsEntity>
        RxView.clicks(iv_back).subscribe({finish()})
        (photoview as ConvenientBanner<DecPicBean.PicsEntity>).setPages(object :CBViewHolderCreator<PhotoPreViewHolderView>{
            override fun createHolder(): PhotoPreViewHolderView {
                return PhotoPreViewHolderView()
            }

        }, datas).isCanLoop = false
        photoview.stopTurning()
        photoview.onPageChangeListener = object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                updateIndex(position)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        }
    }

    override fun showLoading() {
    }

    override fun launchActivity(intent: Intent?) {
    }

    override fun hideLoading() {
    }

    override fun killMyself() {
    }

    override fun showMessage(message: String?) {
    }

    private fun updateIndex(pos: Int) {
        val str = (pos + 1).toString() + "/" + datas.size
        tv_title.setText(str)
    }
    companion object{
        fun startPhotoViewActivity(context: Context, lists: List<DecPicBean.PicsEntity>) {
            val intent = Intent(context, PhotoPreViewActivity::class.java)
            intent.putExtra(AppKey.EXTRA_DATA, lists as Serializable)
            context.startActivity(intent)
        }
    }

    override fun useFragment(): Boolean {
        return false
    }
}
